/**
 * @author : andy
 * @description : socket客户端
 */
import ClientManager from "../utils/manager";
import Socket, { type SocketParams } from "./socket";
export default class SocketClient {
  /** socket实例管理类 */
  private socketManager: ClientManager<Socket>;
  constructor () {
    this.socketManager = new ClientManager();
  }
  create (options: SocketParams) {
    const socket = new Socket(options , this.socketManager);
    this.socketManager.add(socket);
    return this.socketManager.get(socket.id) as Socket;
  }
  delete (socket: Socket) {
    this._delete(socket.id);
    socket.disconnect();
  }
  private _delete (id: string) {
    this.socketManager.delete(id);
  }
  get (id: string) {
    this.socketManager.get(id);
  }
  clear () {
    const instances = this.socketManager.getAll();
    for (const instance of instances) {
      this.delete(instance);
    }
  }
  has (id: string) {
    return this.socketManager.has(id);
  }
}